package com.cloud.drore.eboss.supplier.mapper.provider;

import com.cloud.drore.eboos.common.entity.supplier.po.CombinationProductQueryPo;
import com.cloud.drore.eboss.supplier.entity.CombinationProduct;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/8 14:43 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class CombinationProductProvider {

    private static String findCombinationProductByParamAndPoiSql = "select pro.id as id,pro.product_name,pro.product_crowd,pro.stock_method,pro.payment_type," +
            " pro.instruction,pro.common_product_id," +
            " pro.refund_fee,pro.refund_model,pro.term_of_validity_start_time,pro.term_of_validity_end_time,pro.status," +
            "pro.refund_one_day_ago,pro.refund_two_days_ago,pro.refund_three_days_ago,pro.product_num,pro.is_real_name,pro.booking_notice," +
            "price.sales_price,price.settlement_price,price.start_time,price.end_time" +
            " from eboos_product commomProduct " +
            " left join eboos_combination_product_price price on commomProduct.product_id = price.combination_product_id  " +
            " inner join eboos_combination_product pro on commomProduct.id = common_product_id " +
            " left join eboos_product_consume_relation consumeRelation on consumeRelation.product_id = commomProduct.id " +
            "  where pro.is_deleted = 0 and commomProduct.product_type= 'COMBINATION_PRODUCT' ";

    private static String findCombinationProductByParamSql = "select pro.id as id,pro.product_name,pro.product_crowd,pro.stock_method,pro.payment_type," +
            " pro.instruction,pro.common_product_id," +
            "pro.refund_fee,pro.refund_model,pro.term_of_validity_start_time,pro.term_of_validity_end_time,pro.status," +
            "pro.refund_one_day_ago,pro.refund_two_days_ago,pro.refund_three_days_ago,pro.product_num,pro.is_real_name,pro.booking_notice," +
            "price.sales_price,price.settlement_price,price.start_time,price.end_time" +
            " from eboos_combination_product pro " +
            " left join eboos_combination_product_price price on pro.id = price.combination_product_id  " +
            "  where pro.is_deleted = 0 ";

    private static String selectCombinationProductByIdSql =  "select pro.id as id,pro.product_name,pro.product_crowd,pro.stock_method,pro.payment_type," +
            " pro.instruction,pro.common_product_id," +
            "pro.refund_fee,pro.refund_model,pro.term_of_validity_start_time,pro.term_of_validity_end_time,pro.status," +
            "IFNULL(pro.refund_one_day_ago,0) AS refund_one_day_ago,IFNULL(pro.refund_two_days_ago,0) AS refund_two_days_ago," +
            "IFNULL(pro.refund_three_days_ago,0) AS refund_three_days_ago,IFNULL(pro.fixed_rate,0) AS fixed_rate," +
            "pro.product_num,pro.is_real_name,pro.booking_notice," +
            "price.sales_price,price.settlement_price,price.start_time,price.end_time " +
            " from eboos_combination_product pro " +
            " left join eboos_combination_product_price price on pro.id = price.combination_product_id " +
            "  where pro.is_deleted = 0 " +
            " and pro.id = ";

    private static String findCombinationProductByParamAndPoiDealerSql = "select pro.id as id,pro.product_name,pro.product_crowd,pro.stock_method,pro.payment_type," +
            " pro.instruction,pro.common_product_id," +
            " pro.refund_fee,pro.refund_model,pro.term_of_validity_start_time,pro.term_of_validity_end_time,pro.status," +
            "pro.refund_one_day_ago,pro.refund_two_days_ago,pro.refund_three_days_ago,pro.product_num,pro.is_real_name,pro.booking_notice," +
            "price.sales_price,price.settlement_price,price.start_time,price.end_time" +
            " from eboos_product commomProduct " +
            " left join eboos_combination_product_price price on commomProduct.product_id = price.combination_product_id  " +
            " inner join eboos_combination_product pro on commomProduct.id = common_product_id " +
            "  INNER JOIN eboos_distributor_product_association disAsso " +
            "  ON  commomProduct.id = disAsso.main_id " +
            " left join eboos_product_consume_relation consumeRelation on consumeRelation.product_id = commomProduct.id " +
            "  where pro.is_deleted = 0 and commomProduct.product_type= 'COMBINATION_PRODUCT' ";


    public CombinationProductProvider(){

    }

    public String selectCombinationProductById(Long id){
        String sql = selectCombinationProductByIdSql +"'"+id+"' ";
        return sql;
    }


    public String findCombinationProductByParam(CombinationProductQueryPo productQueryPo){
        String sql = findCombinationProductByParamSql;
        if(StringUtils.isNotBlank(productQueryPo.getName()))
            sql += " and pro.product_name like'%" + productQueryPo.getName() + "%'";
        if(StringUtils.isNotBlank(productQueryPo.getStatus()))
            sql += " and pro.status='" + productQueryPo.getStatus() + "'";
        return sql;
    }


    public String findCombinationProductByParamAndPoi(CombinationProductQueryPo productQueryPo){
        String sql = findCombinationProductByParamAndPoiSql;
        if(StringUtils.isNotBlank(productQueryPo.getName()))
            sql += " and pro.product_name like'%" + productQueryPo.getName() + "%'";
        if(StringUtils.isNotBlank(productQueryPo.getStatus()))
            sql += " and pro.status='" + productQueryPo.getStatus() + "'";
        if(productQueryPo.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + productQueryPo.getPoiId() + "'";
        if(productQueryPo.getSupplierId() != null)
            sql += " and consumeRelation.merchant_id='" + productQueryPo.getSupplierId() + "'";
        sql += " group by commomProduct.product_id order by pro.created_date desc";
        return sql;
    }


    public String updateCombinationProductByParam(CombinationProduct combinationProduct){
        int count = 0;
        String sql = " update eboos_combination_product set ";
        if (StringUtils.isNotBlank(combinationProduct.getProductName())) {
            sql += " product_name='" + combinationProduct.getProductName() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getInstruction())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " instruction='" + combinationProduct.getInstruction() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getStockMethod())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " stock_method ='" + combinationProduct.getStockMethod() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getRefundModel())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_model ='" + combinationProduct.getRefundModel() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getRefundFee())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_fee ='" + combinationProduct.getRefundFee() + "'";
            count++;
        }
        if (combinationProduct.getRefundThreeDaysAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_three_days_ago =" + combinationProduct.getRefundThreeDaysAgo() + "";
            count++;
        }
        if (combinationProduct.getRefundOneDayAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_one_day_ago =" + combinationProduct.getRefundOneDayAgo() + "";
            count++;
        }
        if (combinationProduct.getRefundThreeDaysAgo()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_two_days_ago =" + combinationProduct.getRefundTwoDaysAgo() + "";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getStatus())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " status ='" + combinationProduct.getStatus() + "'";
            count++;
        }
        if (combinationProduct.getFixedRate()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " fixed_rate ='" + combinationProduct.getFixedRate() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(combinationProduct.getProductNum())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " product_num ='" + combinationProduct.getProductNum() + "'";
            count++;
        }
        if (combinationProduct.getIsRealName()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_real_name ='" + combinationProduct.getIsRealName() + "'";
            count++;
        }
        sql += " where id='" + combinationProduct.getId() + "'";
        return sql;
    }

    public String selectRejectInfoByProId(Long productId){
        String sql = " select " +
                "  pro.reject_reason,account.account_tel,account.account_name from " +
                "  eboos_combination_product pro " +
                "  left join eboos_account account " +
                "  on pro.reject_user = account.id" +
                " where pro.id = '"+productId+"'  ";
        return sql;
    }


    public String findCombinationProductByParamAndPoiSupplier(CombinationProductQueryPo productQueryPo){
        String sql = findCombinationProductByParamAndPoiSql;
        if(StringUtils.isNotBlank(productQueryPo.getName()))
            sql += " and pro.product_name like'%" + productQueryPo.getName() + "%'";
        if(StringUtils.isNotBlank(productQueryPo.getStatus()))
            sql += " and pro.status='" + productQueryPo.getStatus() + "'";
        if(productQueryPo.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + productQueryPo.getPoiId() + "'";
        if(productQueryPo.getSupplierId() != null)
            sql += " and consumeRelation.merchant_id='" + productQueryPo.getSupplierId() + "'";
        sql += " group by commomProduct.product_id order by pro.created_date desc";
        return sql;
    }


    public String findCombinationProductByParamAndPoiDealer(CombinationProductQueryPo productQueryPo){
        String sql = findCombinationProductByParamAndPoiDealerSql;
        if(StringUtils.isNotBlank(productQueryPo.getName()))
            sql += " and pro.product_name like'%" + productQueryPo.getName() + "%'";
        if(StringUtils.isNotBlank(productQueryPo.getStatus()))
            sql += " and pro.status='" + productQueryPo.getStatus() + "'";
        if(productQueryPo.getPoiId()!=null)
            sql += " and consumeRelation.poi_id ='" + productQueryPo.getPoiId() + "'";
        if(productQueryPo.getSupplierId() != null)
            sql += " and consumeRelation.merchant_id='" + productQueryPo.getSupplierId() + "'";
        sql += " group by commomProduct.product_id order by pro.created_date desc";
        return sql;
    }

}